//
//  SEChangePsdViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SEChangePsdViewController: SEBaseViewController {

    @IBOutlet weak var confirmBtnView: SERegistrationConfirmBtnView! {
        didSet {
            confirmBtnView.title = "确认修改"
            confirmBtnView.btnBgColor = UIColor.init(hex: "03C691")
            confirmBtnView.btnEnableBgColor = UIColor.init(hex: "AEE5D3")
            confirmBtnView.delegate = self
        }
    }
    @IBOutlet weak var oldPasswordTextView: SEAuthTextFildView! {
        didSet {
            oldPasswordTextView.viewType = .oldPassword
            oldPasswordTextView.isHiddenLogo = true
        }
    }

    @IBOutlet weak var passwordTextView: SEAuthTextFildView! {
        didSet {
            passwordTextView.viewType = .password
            passwordTextView.isHiddenLogo = true
        }
    }
    @IBOutlet weak var confirmTextView: SEAuthTextFildView! {
        didSet {
            confirmTextView.viewType = .confirmPsd
            confirmTextView.isHiddenLogo = true
        }
    }

    var viewModel = SEAuthViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        addEndEditingTap()

        viewModel.changePsdHandler = { status in
            switch status {
            case .success:
                self.handlerChangePsdSuccess()
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func handlerChangePsdSuccess() {
        UserTools.removeAllUserInfo()
        SEHUD.show(message: "修改成功, 请重新登录")
        self.navigationController?.popToRootViewController(animated: true)
    }

}

// MARK: - SERegistrationConfirmBtnViewDelegate

extension SEChangePsdViewController: SERegistrationConfirmBtnViewDelegate {
    func registrationConfirmBtnViewAction(view: SERegistrationConfirmBtnView) {
        self.view.endEditing(true)
        viewModel.oldPassword = oldPasswordTextView.textField.text!
        viewModel.password = passwordTextView.textField.text!
        viewModel.confirmPassword = confirmTextView.textField.text!
        requestChangePsd()
    }
}

// MARK: - APIRequest

extension SEChangePsdViewController {

    private func requestChangePsd() {
        viewModel.requestChangePassword()
    }
}
